package com.itheima.day10;

import com.itheima.util.BallUtil;

import java.util.*;

/*
1_ 5000注不允许重复
2_ 蓝色球 连号最多出现连三，连四及以上不允许出现
3_ 统计5000个号中号码10,20,30分别出现的次数(红球 , 篮球)
4_ 5000个号码中，哪个号码出现的次数最多？(红球 ,篮球 )
 */
public class Test04 {
    public static void main(String[] args) {
        TreeSet<int[]> set = new TreeSet<>((arr1, arr2) -> {
            int result = arr1[0] - arr2[0];
            result = result == 0 ? arr1[1] - arr2[1] : result;
            result = result == 0 ? arr1[2] - arr2[2] : result;
            result = result == 0 ? arr1[3] - arr2[3] : result;
            result = result == 0 ? arr1[4] - arr2[4] : result;
            result = result == 0 ? arr1[5] - arr2[5] : result;
            result = result == 0 ? arr1[6] - arr2[6] : result;
            return result;
        });
        while (set.size() < 5000) {
            int[] arr = new int[7];
            BallUtil ballUtil = new BallUtil();
            ArrayList<Integer> redBalls = ballUtil.getRedBalls();
            Integer blueBall = ballUtil.getBlueBall();
            Collections.sort(redBalls);
            //System.out.println("红球" + redBalls);
            for (int j = 0; j < redBalls.size(); j++) {
                arr[j] = redBalls.get(j);
            }
            arr[6] = blueBall;
            //System.out.println("蓝球" + blueBall);
            //System.out.println("本期开奖"+ Arrays.toString(arr));
            set.add(arr);
        }

        System.out.println("5000次生成了" + set.size());
        for (int[] ints : set) {
            System.out.println(Arrays.toString(ints));
        }
        int count10 = 0;
        int count20 = 0;
        int count30 = 0;
        for (int[] ints : set) {
            for (int i = 0; i < ints.length - 1; i++) {
                if (ints[i] == 10) {
                    count10++;
                }
                if (ints[i] == 20) {
                    count20++;
                }
                if (ints[i] == 30) {
                    count30++;
                }
            }
          /*if(ints[6]==10){
              count10++;
          }*/

        }
        System.out.println("红球中10出现的次数" + count10);
        System.out.println("红球中20出现的次数" + count20);
        System.out.println("红球中30出现的次数" + count30);


        //先统计一下每个求出现的次数
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int[] ints : set) {
            for (int i = 0; i < ints.length - 1; i++) {
                int ball = ints[i];
                if (map.containsKey(ball)) {
                    map.put(ball, map.get(ball) + 1);
                } else {
                    map.put(ball, 1);
                }
            }
        }

       /* map.forEach((k, v) -> System.out.println(k + "出现了" + v + "次"));*/
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        TreeSet<Map.Entry<Integer, Integer>> jieguo = new TreeSet<>((entry1, entry2) -> {
            int result = entry1.getValue() - entry2.getValue();
            result = result == 0 ? entry1.getKey() - entry2.getKey() : result;
            return result;
        });
        jieguo.addAll(entries);
        for (Map.Entry<Integer, Integer> entry : jieguo) {
            System.out.println(entry.getKey()+"出现了"+entry.getValue());
        }
    }


}
